<template>
  <div>
    <BasicTable @register="registerTable" size="small">
      <template #toolbar>
        <a-button preIcon="ant-design:plus-circle-outlined"  type="primary"
        v-auth="'Basic.ProductManagement.Create'"
        @click="openCreateProductModal">
          {{ t('common.createText') }}
        </a-button>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: t('common.editText'),
              auth: 'Basic.ProductManagement.Update',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              label: t('common.delText'),
               auth: 'Basic.ProductManagement.Delete',
              popConfirm: {
                title: t('common.askDelete'),
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>

    <CreateProduct
      @register="registerCreateProductModal"
      @reload="reload"
      :bodyStyle="{ 'padding-top': '0' }"
    />
    <UpdateProduct
      @register="registerUpdateProductModal"
      @reload="reload"
      :bodyStyle="{ 'padding-top': '0' }"
    />

  </div>
</template>
  
<script lang="ts">
import { defineComponent } from 'vue';
import { BasicTable, TableAction, useTable } from '/@/components/Table';
import { tableColumns, searchFormSchema, getTableListAsync,deleteAsync } from '/@/views/basic/product/product';
import { useI18n } from '/@/hooks/web/useI18n';
import { useModal } from '/@/components/Modal';
import CreateProduct from './CreateProduct.vue';
import UpdateProduct from './UpdateProduct.vue';

export default defineComponent({
  name: 'Product',
  components: {
    BasicTable,
    TableAction,
    CreateProduct,
    UpdateProduct
  },
  setup() {
    const { t } = useI18n();
    const [registerCreateProductModal, { openModal: openCreateProductModal }] = useModal();
    const [registerUpdateProductModal, { openModal: openUpdateProductModal }] = useModal();

    const [registerTable, { reload }] = useTable({
      columns: tableColumns,
      formConfig: {
        labelWidth: 70,
        schemas: searchFormSchema,
      },
      api: getTableListAsync,
      showTableSetting: true,
      useSearchForm: true,
      bordered: true,
      canResize: true,
      showIndexColumn: true,
      immediate: true,
      scroll: { x: true },
      actionColumn: {
          width: 220,
          title: t('common.action'),
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
    });

    function handleEdit(record: Recordable) {
        openUpdateProductModal(true, {
          record: record,
        });
      }

      async function handleDelete(record: Recordable) {
        await deleteAsync({ id: record.id });
        await reload();
      }

    return {
      t,
      registerTable,
      reload,
      handleEdit,
      handleDelete,
      registerCreateProductModal,
      registerUpdateProductModal,
      openCreateProductModal,
    };
  },
});
</script>
  